<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/2
 * Time: 14:34
 */
namespace Huangzikun\hawkeye_spider;
use Redis;

class RedisClass extends BaseClass
{
    public $host;
    public $port;
    public $apiAddress;
    public $spiderInfo;

    protected $fields = [
        'connected_clients',
        'used_memory_rss',
        'used_memory_peak',
        'total_connections_received',
        'instantaneous_ops_per_sec',
        'instantaneous_input_kbps',
        'instantaneous_output_kbps',
        'rejected_connections',
        'expired_keys',
        'evicted_keys',
        'keyspace_hits',
        'keyspace_misses'
    ];

    protected $listKey = 'sub_id:';

    public function grab()
    {
        $redis = $this->connection();
        if($redis == null){
            return ;
        }

        $info = (array)$redis->info();
        if(!empty($info)){
            foreach ($this->fields as $field){
                $this->spiderInfo[$field] = $info[$field];
            }
        }

        $listLen = $redis->lLen($this->listKey);
        $this->spiderInfo['list_len'] = intval($listLen);
        $redis->close();
    }

    /**
     * 连接
     * @return null|Redis
     */
    protected function connection()
    {
        $redis = new Redis();
        if($redis->connect($this->host, $this->port)){
            return $redis;
        }
        return null;

    }
}